#!/usr/bin/python
# -*- coding: utf-8 -*-
# @version        : 1.0
# @Create Time    : 2024/9/6 11:11
# @File           : __init__
# @IDE            : PyCharm
# @desc           : 描述

from fastapi import APIRouter, Depends
from utils.response import SuccessResponse
from . import schemas, crud, params
from core.dependencies import IdList
from apps.base.auth.validate import AllUserAuth, Auth
from apps.check.qar.crud import QARDal
from apps.check.asr.crud import ASRDal
from apps.check.order.crud import OrderDal


app = APIRouter()


@app.get("/rebut", summary="获取驳回历史")
async def get_rebut(
        p: params.RebutParams = Depends(),
        auth: Auth = Depends(AllUserAuth())
):
    datas, count = await crud.RebutDal(auth.db).get_datas(
        **p.dict(),
        v_schema=schemas.RebutSimpleOut,
        v_return_count=True
    )
    return SuccessResponse(datas, count=count)


@app.post("/rebut", summary="创建驳回历史")
async def create_rebut(
        data: schemas.Rebut,
        auth: Auth = Depends(AllUserAuth())
):
    data.create_user = auth.user.user_str
    if data.rebut_type == "QAR":
        obj = await QARDal(auth.db).get_data(qar_no=data.rebut_form_no)
    elif data.rebut_type == "ASR":
        obj = await ASRDal(auth.db).get_data(asr_no=data.rebut_form_no)
    else:
        obj = await OrderDal(auth.db).get_data(order_no=data.rebut_form_no)

    obj.status = data.after_status
    await crud.RebutDal(auth.db).flush(obj)
    return SuccessResponse(await crud.RebutDal(auth.db).create_data(data))


# @app.put("/rebut/{data_id}", summary="更新驳回历史")
# async def put_rebut(
#         data_id: int,
#         data: schemas.Rebut,
#         auth: Auth = Depends(AllUserAuth())

# ):
#     return SuccessResponse(await crud.RebutDal(auth.db).put_data(data_id, data=data))


# @app.delete("/rebut", summary="批量删除驳回历史", description="硬删除")
# async def delete_rebut(ids: IdList = Depends(), auth: Auth = Depends(AllUserAuth())):
#     await crud.RebutDal(auth.db).delete_datas(ids=ids.ids)
#     return SuccessResponse("删除成功")


@app.get("/rebut/options", summary="获取可驳回状态")
async def get_rebut_options(
        p: params.RebutParams = Depends(),
        auth: Auth = Depends(AllUserAuth())
):
    # 根据不同表单类型，获取可驳回状态
    return SuccessResponse(await crud.RebutDal(auth.db).get_select_datas(**p.dict()))